<?php

namespace app\common\model\live;

use app\common\model\Product\Product as ProductProduct;
use app\common\model\Subject\Subject;
use think\Model;

class Product extends Model
{
    // 表名
    protected $name = 'live_product';
    // 忽略数据表不存在的字段
    protected $field = true;
    // 设置返回的结果类型
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [
        'live',
        'relation_object',
        'type_text'
    ];
    public function live()
    {
        return $this->belongsTo(Live::class,'liveid','id',[],'left')->setEagerlyType(0);
    }
    public function getRelationObjectAttr($value,$data)
    {
        if($data['type']=='subject')
        {
            return $this->hasOne(Subject::class,'id','relation')->find();
        }else if($data['type']=='product')
        {
            return $this->hasOne(ProductProduct::class,'id','relation')->find();
        }
        // return $this->belongsTo(Live::class,'relation','id',[],'left')->setEagerlyType(0);
    }
    public function getTypeList()
    {
        return ['subject' => __('Subject'), 'product' => __('Product')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }
}
